package com.zxy.springboot_vue.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxy.springboot_vue.config.AjaxResult;
import com.zxy.springboot_vue.entity.Role;
import com.zxy.springboot_vue.entity.UserRole;
import com.zxy.springboot_vue.service.IUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lenovo
 * @since 2021-10-19
 */
@RestController
@RequestMapping("/user-role")
public class UserRoleController {

    @Autowired
    private IUserRoleService userRoleService;

    @GetMapping("getRoleByUid")
    public AjaxResult getRoleByUid(Integer uid) {
        QueryWrapper<UserRole> wrapper = null;

        if (uid != null) {
            wrapper = new QueryWrapper();
            wrapper.eq("uid", uid);
        }
        List<UserRole> list = userRoleService.list(wrapper);
        return AjaxResult.ok(list);
    }

    @PostMapping("addRole")
    private AjaxResult addRole(@RequestBody UserRole userRole) {
        System.err.println(userRole);
        QueryWrapper<UserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("uid", userRole.getUid());
        boolean b = userRoleService.remove(wrapper);

        List<Integer> rids = userRole.getRids();
        for (Integer rid : rids) {
            UserRole role = new UserRole();
            role.setRid(rid);
            role.setUid(userRole.getUid());
            userRoleService.saveOrUpdate(role);

        }
        return AjaxResult.ok();

    }
}
